package org.tzeng.html;

import com.alibaba.fastjson.JSON;
import org.tzeng.html.bean.ConfigInfo;
import org.tzeng.html.bean.PageInfo;
import org.tzeng.html.extra.CtripExtra;
import org.tzeng.html.extra.LYExtra;
import org.tzeng.html.extra.TuniuExtra;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by tzeng on 2015-12-13.
 */
public class Extra {

    /*
     * 模板信息，结构为 <k, v>，k为站点主页，v为模板信息
     */
    private static Map<String, ConfigInfo> templetInfo = null;

    /*
     * init 模板变量
     */
    static {
        String line = "[{\"sid\":\"ctrip\",\"icon_url\":\"http://www.ctrip.com/favicon.ico\",\"site_url\":\"http://www.ctrip.com\",\"site_name\":\"携程旅行网\",\"gtravel\":{\"product_name\":\"title\",\"product_num\":\"li.product_num\",\"product_city\":\"span.city\",\"product_to_city\":\"meta[name=\\\"location\\\"]\",\"provider_href\":\"a.provider_name\",\"provider_name\":\"a.provider_name\",\"special_info\":\"dl.special_info dd span\",\"simple_route\":\"div#simple_route_div\",\"pm_recommend\":\"div.pm_recommend ul li\",\"js_calendar\":\"div#js_calendar strong\",\"detail_calendar\":\"table.detail_calendar_table td span\",\"today_price\":\"strong#J_total_price\",\"comment_wrap\":\"div#js_main_price_wrap div a.score\"},\"ftravel\":{\"product_name\":\"title\",\"product_num\":\"li.product_num\",\"product_city\":\"span.city\",\"product_to_city\":\"meta[name=\\\"location\\\"]\",\"provider_href\":\"a.provider_name\",\"provider_name\":\"a.provider_name\",\"special_info\":\"dl.special_info dd span\",\"simple_route\":\"div#simple_route_div\",\"pm_recommend\":\"div.pm_recommend ul\",\"js_calendar\":\"h4.calen_year\",\"detail_calendar\":\"table.calendar_right td a\",\"today_price\":\"strong#js-product-min-price\",\"comment_wrap\":\"div.comment_wrap a.score\"}},{\"sid\":\"tuniu\",\"icon_url\":\"http://www.tuniu.com/favicon.ico\",\"site_url\":\"http://www.tuniu.com\",\"site_name\":\"途牛旅游网\",\"gtravel\":{\"product_name\":\"title\",\"product_num\":\"span.c_f80\",\"product_city\":\"div#startCity p\",\"product_to_city\":\"p.select_result\",\"provider_href\":\"a.provider_name\",\"provider_name\":\"a.provider_name\",\"special_info\":\"dl.special_info dd span\",\"simple_route\":\"div#simple_route_div\",\"pm_recommend\":\"div.pm_recommend ul\",\"js_calendar\":\"div#js_calendar strong\",\"detail_calendar\":\"table.detail_calendar_table td span\",\"today_price\":\"strong#J_total_price\",\"comment_wrap\":\"span.dp-score b\"}},{\"sid\":\"ly.com\",\"icon_url\":\"http://www.ly.com/favicon.ico\",\"site_url\":\"http://www.ly.com\",\"site_name\":\"同程旅游\",\"gtravel\":{\"product_name\":\"title\",\"product_num\":\"commonProId\",\"product_city\":\"StartCityName\",\"product_to_city\":\"DestinationName\",\"provider_href\":\"\",\"provider_name\":\"\",\"special_info\":\"dd.label_maxheight span.red_btn\",\"simple_route\":\"dd span.layer\",\"pm_recommend\":\"div.recommend-content p\",\"js_calendar\":\"div.calendar-head-box\",\"detail_calendar\":\"div.calendar-container table td span\",\"today_price\":\"li#PriceInfo div.price b\",\"comment_wrap\":\"span.dp-score b\"}}]";
        List<ConfigInfo> lists = JSON.parseArray(line, ConfigInfo.class);
        if(null == templetInfo){
            templetInfo = new HashMap<String, ConfigInfo>();
            for (ConfigInfo ci : lists){
                templetInfo.put(ci.getSite_url(), ci);
            }
        }
    }

    public static PageInfo parseHtml(String url, String str){
        PageInfo pageInfo = null;
        if (url.contains("ctrip")){
//            if (str.indexOf("/*详情页配置*/") > 0){
//                File file = new File("E:\\html\\ctrip_vacation\\p2365168s428.html");
//                pageInfo = CtripExtra.extraGInfo(file, templetInfo.get("http://www.ctrip.com").getGtravel());
//                System.out.print(pageInfo.toString());
//            }else if (str.indexOf("FRTPage.extend(") > 0){
//                File file = new File("E:\\html\\ctrip_free\\p2104020s2.html");
//                pageInfo = CtripExtra.extraFInfo(file, templetInfo.get("http://www.ctrip.com").getFtravel());
//                System.out.print(pageInfo.toString());
//            }
        } else if (url.contains("tuniu")){
            //File file = new File("E:\\html\\tuniu\\5011509");
            pageInfo = TuniuExtra.extraGInfo(str, templetInfo.get("http://www.tuniu.com").getGtravel());
            System.out.print(pageInfo.toString());
            pageInfo.setProduct_num(url.substring(url.lastIndexOf("/") + 1));
        } else if (url.contains("ly.com")){
            //File file = new File("E:\\html\\ly\\t1j3p20094c214.html");
            pageInfo = LYExtra.extraGInfo(str, templetInfo.get("http://www.ly.com").getGtravel());
            System.out.print(pageInfo.toString());
        }



        pageInfo.setProduct_url(url);

        return pageInfo;
    }

    public static void main(String[] args){
        String url = "";
        // grouptravel
        url = "http://vacations.ctrip.com/grouptravel/p1823994s428.html";
        Extra.parseHtml(url, url);

        // freetravel
        url = "http://vacations.ctrip.com/freetravel/p4339528s2.html";
        Extra.parseHtml(url, url);

        // tuniu
        /*url = "http://www.tuniu.com/tours/5011500";
        Extra.parseHtml(url, url);*/

        // ly.com
        url = "http://gny.ly.com/line/t1j1p13118c0.html";
        Extra.parseHtml(url, url);
    }
}
